package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.Stock;

public class StockDAOImpl extends BaseHibernateDAOImpl<Stock> implements StockDAO {

	public Stock getStockByStockID(String stockID) {
		Stock stock;
		try {
			stock = (Stock) getSession().get(Stock.class, stockID);

		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return stock;
	}

	@SuppressWarnings("unchecked")
	public List<Stock> getAllStock() {
		List<Stock> stocks;
		try {
			Query query = getSession().getNamedQuery("AllStockQuery");
			stocks = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return stocks;
	}

	@SuppressWarnings("unchecked")
	public List<Stock> getAllStockByPage(int currentPage, int lineSize) {
		List<Stock> stocks;
		try {
			Query query = getSession().getNamedQuery("AllStockByPageQuery");
			query.setFirstResult((currentPage - 1) * lineSize);
			query.setMaxResults(lineSize);
			stocks = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return stocks;
	}
	

}
